package com.cskaoyan.service.mall;

import com.cskaoyan.bean.bo.mall.OrderShipBo;
import com.cskaoyan.bean.po.ohter.MarketOrderGoods;
import com.cskaoyan.bean.po.ohter.MarketOrderGoodsExample;
import com.cskaoyan.bean.po.ohter.MarketUser;
import com.cskaoyan.bean.po.wxorder.MarketComment;
import com.cskaoyan.bean.po.wxorder.MarketCommentExample;
import com.cskaoyan.bean.po.wxorder.MarketOrder;
import com.cskaoyan.bean.po.wxorder.MarketOrderExample;
import com.cskaoyan.bean.vo.mall.OrderDetailVo;
import com.cskaoyan.bean.vo.mall.OrderListInfo;
import com.cskaoyan.bean.vo.mall.OrderListVo;
import com.cskaoyan.mapper.wxorder.MarketCommentMapper;
import com.cskaoyan.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.mapper.wxorder.MarketOrderMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.util.beanutils.BeanUtil;
import com.cskaoyan.util.common.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author Ajun de BBC
 * @Date 2022/7/17 16:53
 * @Version 1.5
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    MarketOrderMapper marketOrderMapper;
    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;
    @Autowired
    MarketUserMapper marketUserMapper;
    @Autowired
    MarketCommentMapper marketCommentMapper;

    /**
     *@Description : 订单界面的显示
     *@Param :
     *@Return :
     */
    @Override
    public OrderListVo orderList(Integer page, Integer limit, Short[] orderStatusArray, String sort, String order, Integer orderId, String start, String end, Integer userId, String orderSn) throws ParseException {
        PageHelper.startPage(page, limit);
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        marketOrderExample.setOrderByClause(sort + " " + order);
        MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        //拼接订单编号模糊查询
        orderSn = (orderSn == null) ? "%" : "%" + orderSn + "%";
        criteria.andOrderSnLike(orderSn);
        //判断查询时的日期起止 开始和终止时间同时有值才可查询该时间段里的订单
        if( !StringUtils.isEmpty(start) && !StringUtils.isEmpty(end)){
            Date startTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(start);
            Date endTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(end);
            criteria.andAddTimeBetween(startTime, endTime);
        }
        //判断订单状态，订单状态可以选择多个，用数组接受转换
        if(orderStatusArray != null){
            ArrayList<Short> shortList = new ArrayList<>();
            for (Short aShort : orderStatusArray) {
                shortList.add(aShort);
            }
            criteria.andOrderStatusIn(shortList);
        }
        //判断用户ID
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(marketOrderExample);

        List<OrderListInfo> orderListInfos = new ArrayList<>();
        for (MarketOrder marketOrder : marketOrders) {
            OrderListInfo orderListInfo = new OrderListInfo();
            BeanUtil.copyBean(marketOrder, orderListInfo);
            orderListInfos.add(orderListInfo);
        }

        PageInfo<MarketOrder> marketOrderPageInfo = new PageInfo<>(marketOrders);
        Long total = marketOrderPageInfo.getTotal();
        int pages = marketOrderPageInfo.getPages();

        return new OrderListVo(total.intValue(), pages, limit, page, orderListInfos);
    }

    /**
     *@Description : 显示订单的详细情况
     *@Param :
     *@Return :
     */
    @Override
    public OrderDetailVo orderDetail(Integer orderId) {
        //订单商品详情
        MarketOrderGoodsExample marketOrderGoodsExample = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = marketOrderGoodsExample.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        List<MarketOrderGoods> orderGoods = marketOrderGoodsMapper.selectByExample(marketOrderGoodsExample);

        //订单详情
        MarketOrder order = marketOrderMapper.selectByPrimaryKey(orderId);
        while (order.getDeleted() == true){
            return null;
        }

        //用户详情-----只有昵称和头像
        Integer userId = order.getUserId();
        MarketUser user = marketUserMapper.selectByPrimaryKey(userId);

        return new OrderDetailVo(orderGoods,user,order);
    }

    /**
     *@Description : 订单发货
     *@Param :
     *@Return :
     */
    @Override
    public void orderShip(OrderShipBo orderShipBo) {
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
        criteria.andIdEqualTo(orderShipBo.getOrderId());
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setShipChannel(orderShipBo.getShipChannel());
        marketOrder.setShipSn(orderShipBo.getShipSn());
        marketOrder.setOrderStatus(Short.valueOf("301"));
        marketOrderMapper.updateByExampleSelective(marketOrder, marketOrderExample);
    }

    /**
     *@Description : 订单的逻辑删除 当订单状态为101/103/201/202/301/402时，不能删除；当订单状态为102/203/401时订单逻辑删除
     *@Param :
     *@Return :
     */
    @Override
    public int orderDelete(Integer orderId) {
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(orderId);
        Short orderStatus = marketOrder.getOrderStatus();
        if(orderStatus == 101 || orderStatus == 103 || orderStatus == 201 || orderStatus == 202 || orderStatus == 301 || orderStatus == 402){
            return 623;
        }
        if(orderStatus == 102 || orderStatus == 203 || orderStatus == 401){
            MarketOrderExample marketOrderExample = new MarketOrderExample();
            MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
            criteria.andIdEqualTo(orderId);
            marketOrder.setDeleted(true);
            marketOrderMapper.updateByExampleSelective(marketOrder,marketOrderExample);
        }
        return 1;
    }

    /**
     *@Description : 订单申请退款
     *@Param :
     *@Return :
     */
    @Override
    public void orderRefund(Integer orderId, Integer refundMoney) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setOrderStatus(Short.valueOf("203"));
        marketOrder.setRefundAmount(new BigDecimal(refundMoney));
        marketOrder.setAftersaleStatus(Short.valueOf("3"));
        marketOrder.setRefundType("微信退款接口");
        marketOrder.setRefundTime(new Date(System.currentTimeMillis()));
        marketOrder.setUpdateTime(new Date(System.currentTimeMillis()));
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
        criteria.andIdEqualTo(orderId);
        marketOrderMapper.updateByExampleSelective(marketOrder,marketOrderExample);
    }

    /**
     *@Description : 订单的商品回复
     *@Param :
     *@Return :
     */
    //TODO 可能有bug 商品评论板块写完后测试
    @Override
    public Integer orderReply(Integer commentId, String content) {
        MarketComment marketComment = marketCommentMapper.selectByPrimaryKey(commentId);
        if(marketComment.getContent().length() > 0){
            return null;
        }
        marketComment.setAdminContent(content);
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        criteria.andIdEqualTo(commentId);
        marketCommentMapper.updateByExampleSelective(marketComment,marketCommentExample);
        return 1;
    }


}
